package com.munhrasoft.dinamica.business.logic;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.munhrasoft.dinamica.business.entities.Corretor;

@Stateless
public class CorretorEJB {
	
	@PersistenceContext(unitName = "dinamicaPU")
	private EntityManager em;
	
	@SuppressWarnings("unchecked")
	public List<Corretor> fetchAllCorretores(){
	    Query query = em.createNamedQuery("findAllCorretores");
        return query.getResultList();
    }
	
	@SuppressWarnings("unchecked")
	public List<Corretor> findCorretorByName(String name){
		Query query = em.createNamedQuery("findCorretorByNome");
		query.setParameter("nome",name.toLowerCase()+"%");
		return query.getResultList();
	}
	
	public Corretor createCorretor(Corretor corretor){
		em.persist(corretor);
		return corretor;
	}
		
	public Corretor updateCorretor(Corretor corretor){
	    return em.merge(corretor);
	}
	
	public void removeCorretor(Corretor corretor){	    
	    em.remove(em.merge(corretor));		
	}

}
